package com.edu.publisher.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.edu.publisher.bean.TestCourseUcAvgScoreDuration;
import com.edu.publisher.bean.TestPaperUcAvgScoreDuration;
import com.edu.publisher.bean.TestQuestionAnswer;
import com.edu.publisher.bean.TestUcAboutPaperLevel;
import com.edu.publisher.service.TestStatsService;
import com.edu.publisher.util.DateFormatUtil;
import com.edu.publisher.util.StringFormatUtil;

/**
 * Created on 2022/10/20.
 *
 * @author Topus
 */
@RestController
@RequestMapping("/test")
public class TestStatsController {
    @Autowired
    private TestStatsService testStatsService;

    /*
        {
          "status": 0,
          "msg": "",
          "data": {
            "columns": [
              {
                "id": "paperTitle",
                "name": "试卷名"
              },
              {
                "id": "sumUc",
                "name": "考试人数"
              },
              {
                "id": "sumTc",
                "name": "考试次数"
              },
              {
                "id": "avgDuration",
                "name": "平均时长"
              },
              {
                "id": "avgScore",
                "name": "平均分"
              }
            ],
            "rows": [
              {
                "paperTitle": "zhangsan",
                "sumUc": 23,
                "sumTc": 33,
                "avgDuration": 22.0,
                "avgScore": 22.9
              }
            ]
          }
        }
     */
    @RequestMapping("/paper/uasd")
    public String getUasdAboutPaper(@RequestParam(value = "date", defaultValue = "0") Integer date,
        @RequestParam(value = "limit", defaultValue = "10") Integer limit) {
        date = DateFormatUtil.getDefaultDate(date);

        // TODO 使用组件完成报表展示
        // 拼接头部
        StringBuilder sb = new StringBuilder(
            "{\n" + "          \"status\": 0,\n" + "          \"msg\": \"\",\n" + "          \"data\": {\n"
                + "            \"columns\": [\n" + "              {\n" + "                \"id\": \"paperTitle\",\n"
                + "                \"name\": \"试卷名\"\n" + "              },\n" + "              {\n"
                + "                \"id\": \"sumUc\",\n" + "                \"name\": \"考试人数\"\n" + "              },\n"
                + "              {\n" + "                \"id\": \"sumTc\",\n" + "                \"name\": \"考试次数\"\n"
                + "              },\n" + "              {\n" + "                \"id\": \"avgDuration\",\n"
                + "                \"name\": \"平均时长\"\n" + "              },\n" + "              {\n"
                + "                \"id\": \"avgScore\",\n" + "                \"name\": \"平均分\"\n"
                + "              }\n" + "            ],\n" + "            \"rows\": [");

        // 拼接中间过程
        List<TestPaperUcAvgScoreDuration> ucAvgScoreDuration =
            testStatsService.getUcAvgScoreDurationAboutPaper(date, limit);
        for (int i = 0; i < ucAvgScoreDuration.size(); i++) {
            TestPaperUcAvgScoreDuration paperUcAvgScoreDuration = ucAvgScoreDuration.get(i);
            sb.append("{").append("\"paperTitle\": ").append("\"")
                .append(StringFormatUtil.replaceEscape(paperUcAvgScoreDuration.getPaperTitle())).append("\"")
                .append(",").append("\"sumUc\": ").append(paperUcAvgScoreDuration.getSumUc()).append(",")
                .append("\"sumTc\":").append(paperUcAvgScoreDuration.getSumTc()).append(",").append("\"avgDuration\":")
                .append(paperUcAvgScoreDuration.getAvgDuration()).append(",").append("\"avgScore\":")
                .append(paperUcAvgScoreDuration.getAvgScore()).append("}");
            if (i != ucAvgScoreDuration.size() - 1) {
                sb.append(",");
            }
        }

        // 拼接尾部过程
        sb.append("]\n" + "      }\n" + "    }");

        return sb.toString();
    }

    @RequestMapping("/course/uasd")
    public String getUasdAboutCourse(@RequestParam(value = "date", defaultValue = "0") Integer date,
        @RequestParam(value = "limit", defaultValue = "10") Integer limit) {
        date = DateFormatUtil.getDefaultDate(date);

        // TODO 使用组件完成报表展示
        // 拼接头部
        StringBuilder sb = new StringBuilder(
            "{\n" + "          \"status\": 0,\n" + "          \"msg\": \"\",\n" + "          \"data\": {\n"
                + "            \"columns\": [\n" + "              {\n" + "                \"id\": \"courseName\",\n"
                + "                \"name\": \"课程名\"\n" + "              },\n" + "              {\n"
                + "                \"id\": \"sumUc\",\n" + "                \"name\": \"考试人数\"\n" + "              },\n"
                + "              {\n" + "                \"id\": \"sumTc\",\n" + "                \"name\": \"考试次数\"\n"
                + "              },\n" + "              {\n" + "                \"id\": \"avgDuration\",\n"
                + "                \"name\": \"平均时长\"\n" + "              },\n" + "              {\n"
                + "                \"id\": \"avgScore\",\n" + "                \"name\": \"平均分\"\n"
                + "              }\n" + "            ],\n" + "            \"rows\": [");

        // 拼接中间过程
        List<TestCourseUcAvgScoreDuration> ucAvgScoreDuration =
            testStatsService.getUcAvgScoreDurationAboutCourse(date, limit);
        for (int i = 0; i < ucAvgScoreDuration.size(); i++) {
            TestCourseUcAvgScoreDuration paperUcAvgScoreDuration = ucAvgScoreDuration.get(i);
            sb.append("{").append("\"courseName\": ").append("\"")
                .append(StringFormatUtil.replaceEscape(paperUcAvgScoreDuration.getCourseName())).append("\"")
                .append(",").append("\"sumUc\": ").append(paperUcAvgScoreDuration.getSumUc()).append(",")
                .append("\"sumTc\":").append(paperUcAvgScoreDuration.getSumTc()).append(",").append("\"avgDuration\":")
                .append(paperUcAvgScoreDuration.getAvgDuration()).append(",").append("\"avgScore\":")
                .append(paperUcAvgScoreDuration.getAvgScore()).append("}");
            if (i != ucAvgScoreDuration.size() - 1) {
                sb.append(",");
            }
        }

        // 拼接尾部过程
        sb.append("]\n" + "      }\n" + "    }");

        return sb.toString();
    }

    @RequestMapping("/paper/level/uc")
    public String getUcAboutPaperLevel(@RequestParam(value = "date", defaultValue = "0") Integer date,
        @RequestParam(value = "limit", defaultValue = "10") Integer limit,
        @RequestParam(value = "level") String level) {
        date = DateFormatUtil.getDefaultDate(date);

        // TODO 使用组件完成报表展示
        // 拼接头部
        StringBuilder sb = new StringBuilder(
            "{\n" + "          \"status\": 0,\n" + "          \"msg\": \"\",\n" + "          \"data\": {\n"
                + "            \"columns\": [\n" + "              {\n" + "                \"id\": \"paperName\",\n"
                + "                \"name\": \"试卷名\"\n" + "              },\n" + "              {\n"
                + "                \"id\": \"level\",\n" + "                \"name\": \"分段\"\n" + "              },\n"
                + "              {\n" + "                \"id\": \"sumUc\",\n" + "                \"name\": \"考试人数\"\n"
                + "              }\n" + "            ],\n" + "            \"rows\": [");

        // 拼接中间过程
        List<TestUcAboutPaperLevel> ucAboutPaperLevel = testStatsService.getUcAboutPaperLevel(date, limit, level);
        for (int i = 0; i < ucAboutPaperLevel.size(); i++) {
            TestUcAboutPaperLevel ucAboutPaperLevel1 = ucAboutPaperLevel.get(i);
            sb.append("{").append("\"paperName\": ").append("\"")
                .append(StringFormatUtil.replaceEscape(ucAboutPaperLevel1.getPaperName())).append("\"").append(",")
                .append("\"level\": \"").append(ucAboutPaperLevel1.getLevel()).append("\",").append("\"sumUc\":")
                .append(ucAboutPaperLevel1.getSumUc()).append("}");
            if (i != ucAboutPaperLevel.size() - 1) {
                sb.append(",");
            }
        }

        // 拼接尾部过程
        sb.append("]\n" + "      }\n" + "    }");

        return sb.toString();
    }

    /*
    {
      "status": 0,
      "msg": "",
      "data": {
        "columns": [
          {
            "id": "questionName",
            "name": "问题名"
          },
          {
            "id": "sumAc",
            "name": "做题次数"
          },
          {
            "id": "sumCac",
            "name": "正确次数"
          },
          {
            "id": "correctRate",
            "name": "正确率"
          },
          {
            "id": "sumAuc",
            "name": "做题人数"
          },
          {
            "id": "sumCauc",
            "name": "正确人数"
          },
          {
            "id": "correctUserRate",
            "name": "正确人员占比"
          }
        ],
        "rows": [
          {
            "questionName": "zhangsan",
            "sumAc": 23,
            "sumCac": 33,
            "correctRate": 22.0,
            "sumAuc": 22.9,
            "sumCauc": 23,
            "correctUserRate": 22.3
          }
        ]
      }
    }
     */
    @RequestMapping("/question/stats")
    public String getAcCacRateAboutQuestion(@RequestParam(value = "date", defaultValue = "0") Integer date) {

        date = DateFormatUtil.getDefaultDate(date);

        // 拼接头部
        StringBuilder sb =
            new StringBuilder("{\n" + "      \"status\": 0,\n" + "      \"msg\": \"\",\n" + "      \"data\": {\n"
                + "        \"columns\": [\n" + "          {\n" + "            \"id\": \"questionName\",\n"
                + "            \"name\": \"问题名\"\n" + "          },\n" + "          {\n"
                + "            \"id\": \"sumAc\",\n" + "            \"name\": \"做题次数\"\n" + "          },\n"
                + "          {\n" + "            \"id\": \"sumCac\",\n" + "            \"name\": \"正确次数\"\n"
                + "          },\n" + "          {\n" + "            \"id\": \"correctRate\",\n"
                + "            \"name\": \"正确率\"\n" + "          },\n" + "          {\n"
                + "            \"id\": \"sumAuc\",\n" + "            \"name\": \"做题人数\"\n" + "          },\n"
                + "          {\n" + "            \"id\": \"sumCauc\",\n" + "            \"name\": \"正确人数\"\n"
                + "          },\n" + "          {\n" + "            \"id\": \"correctUserRate\",\n"
                + "            \"name\": \"正确人员占比\"\n" + "          }\n" + "        ],\n" + "        \"rows\": [");

        // 拼接中间过程
        List<TestQuestionAnswer> answers = testStatsService.getAcCacRate(date);
        /*
                  {
                    "questionName": "zhangsan",
                    "sumAc": 23,
                    "sumCac": 33,
                    "correctRate": 22.0,
                    "sumAuc": 22.9,
                    "sumCauc": 23,
                    "correctUserRate": 22.3
                  }
         */
        for (int i = 0; i < answers.size(); i++) {
            TestQuestionAnswer answer = answers.get(i);
            sb.append("{").append("\"questionName\": ").append("\"")
                .append(StringFormatUtil.replaceEscape(answer.getQuestionName())).append("\"").append(",")
                .append("\"sumAc\": ").append(answer.getSumAc()).append(",").append("\"sumCac\":")
                .append(answer.getSumCac()).append(",").append("\"correctRate\":").append(answer.getCorrectRate())
                .append(",").append("\"sumAuc\":").append(answer.getSumAuc()).append(",").append("\"sumCauc\":")
                .append(answer.getSumCauc()).append(",").append("\"correctUserRate\":")
                .append(answer.getCorrectUserRate()).append("}");
            if (i != answers.size() - 1) {
                sb.append(",");
            }
        }

        // 拼接尾部过程
        sb.append("]\n" + "      }\n" + "    }");

        return sb.toString();
    }

    @RequestMapping("/question/stats/max/correct/rate")
    public String getMaxCorrectRate(@RequestParam(value = "date", defaultValue = "0") Integer date) {
        date = DateFormatUtil.getDefaultDate(date);

        StringBuilder sb = new StringBuilder("{\"status\": 0,\"data\": ");
        Double maxCorrectRate = testStatsService.getMaxCorrectRate(date);
        sb.append(maxCorrectRate).append("}");
        return sb.toString();
    }

    @RequestMapping("/question/stats/max/correct/user/rate")
    public String getMaxCorrectUserRate(@RequestParam(value = "date", defaultValue = "0") Integer date) {
        date = DateFormatUtil.getDefaultDate(date);

        StringBuilder sb = new StringBuilder("{\"status\": 0,\"data\": ");
        Double maxCorrectRate = testStatsService.getMaxCorrectUserRate(date);
        sb.append(maxCorrectRate).append("}");
        return sb.toString();
    }

    @RequestMapping("/question/stats/max/do/count")
    public String getMaxDoPaperCount(@RequestParam(value = "date", defaultValue = "0") Integer date) {
        date = DateFormatUtil.getDefaultDate(date);

        StringBuilder sb = new StringBuilder("{\"status\": 0,\"data\": ");
        Long maxDoQuestionCount = testStatsService.getMaxDoQuestionCount(date);
        sb.append(maxDoQuestionCount).append("}");
        return sb.toString();
    }
}
